package com.hzg.lanya;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.Manifest;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hzg.lanya.frame.ConnectedFragment;
import com.hzg.lanya.frame.HeartFragment;
import com.hzg.lanya.frame.MtuFragment;
import com.hzg.lanya.frame.MyConfig;
import com.hzg.lanya.frame.ScanFragment;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main2Activity extends AppCompatActivity {

    private TextView n1;
    private TextView n2;
    private TextView n3;
    private TextView n4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

//        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//        Date curDate =  new Date(System.currentTimeMillis());
//        String  rq  =  formatter.format(curDate);
//        int rqnub=Integer.parseInt(rq);
//        if (rqnub>=20230329 || rqnub<20230325){
//            finish();
//        }

        /**
         * 设置为横屏
         */
//        if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        }
        initPermission();
        FrameLayout mFrameLayout=(FrameLayout)findViewById(R.id.frame_content);
        n1=(TextView) findViewById(R.id.a2_name1);
        n2=(TextView) findViewById(R.id.a2_name2);
        n3=(TextView) findViewById(R.id.a2_name3);
        n4=(TextView) findViewById(R.id.a2_name4);
        MyConfig.mScanFragment=new ScanFragment();
//        MyConfig.mConnectedFragment=new ConnectedFragment();
//        MyConfig.mMtuFragment=new MtuFragment();

        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.frame_content,MyConfig.mScanFragment);
        transaction.addToBackStack(null);//返回时有效
        transaction.commit();
        n1.setTextColor(0xff3F51B5);
        n2.setTextColor(0xff000000);
        n3.setTextColor(0xff000000);
        n4.setTextColor(0xff000000);


        n1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n1.setTextColor(0xff3F51B5);
                n2.setTextColor(0xee000000);
                n3.setTextColor(0xee000000);
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.frame_content,MyConfig.mScanFragment);
                transaction.addToBackStack(null);//返回时有效
                transaction.commit();
            }
        });
        n2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n1.setTextColor(0xee000000);
                n2.setTextColor(0xff3F51B5);
                n3.setTextColor(0xee000000);
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.frame_content,new ConnectedFragment());

                //transaction.addToBackStack(null);//返回时有效
                transaction.commit();
            }
        });
        n3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n1.setTextColor(0xee000000);
                n2.setTextColor(0xee000000);
                n3.setTextColor(0xff3F51B5);
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.frame_content,new MtuFragment());
                //transaction.addToBackStack(null);//返回时有效
                transaction.commit();
            }
        });
        n4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n1.setTextColor(0xee000000);
                n2.setTextColor(0xee000000);
                n3.setTextColor(0xff3F51B5);
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.frame_content,new HeartFragment());
                //transaction.addToBackStack(null);//返回时有效
                transaction.commit();
            }
        });

    }





    /**
     * 初始化权限
     */
    private void initPermission(){
        int n = Build.VERSION.SDK_INT;
        if(n >= 23) {
            //动态申请外部SD卡的读写权限
            //获取当前权限状态
            int status1 = ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH);
            int status2 = ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN);
            int status3 = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
            int status4 = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
            int status5 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
            int status6 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            //检测权限
            if(status1 != PackageManager.PERMISSION_GRANTED || status2!= PackageManager.PERMISSION_GRANTED || status3!= PackageManager.PERMISSION_GRANTED
                    || status4!= PackageManager.PERMISSION_GRANTED|| status5!= PackageManager.PERMISSION_GRANTED|| status6!= PackageManager.PERMISSION_GRANTED) {
                //如果没有被允许，动态申请
                ActivityCompat.requestPermissions(this, new String[]{
                        Manifest.permission.BLUETOOTH
                        ,Manifest.permission.BLUETOOTH_ADMIN
                        ,Manifest.permission.ACCESS_FINE_LOCATION
                        ,Manifest.permission.ACCESS_COARSE_LOCATION
                        ,Manifest.permission.READ_EXTERNAL_STORAGE
                        ,Manifest.permission.WRITE_EXTERNAL_STORAGE
                }, 101);
            }
        }
    }




}
